{extend name="property/public/insidePageBase" /}
{block name="title"}地区列表{/block}
{block name="body"}
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form  layui-form-pane" action="">
            <div class="layui-inline">
                <label class="layui-form-label">关键字搜索</label>
                <div class="layui-input-inline">
                    <input type="text" name="keywords" placeholder="请输入标题/关键字/描述" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
            </div>
        </form>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                {if $addAuth && $level <= 3}
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                {/if}
            </div>
        </script>
        <table class="layui-hide" id="systemRegionList" lay-filter="systemRegionList"></table>
        <script type="text/html" id="tableBar">
            <div class="layui-btn-group">
                {if $level <= 2}
                <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" layuimini-content-href="{:url('propertySystemRegionList')}?level={$level}&pid={{ d.id }}" data-title="{{d.name}}下级地区">下级地区</a>
                {/if}
                {if $editAuth}
                <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="edit">编辑</a>
                {/if}
                {if $delAuth}
                <a class="layui-btn layui-btn-sm layui-btn-danger data-count-delete" lay-event="delete">删除</a>
                {/if}
            </div>
        </script>
    </div>
</div>
{/block}
{block name="js"}
{__block__}
<script>
    layui.use(['form', 'table','miniTab'], function () {
        var $ = layui.jquery, form = layui.form, table = layui.table, miniTab=layui.miniTab;
        var tableId = 'systemRegionList';
        table.render({
            elem: '#'+tableId,
            id:tableId,
            url: '{:url("propertySystemRegionList")}',
            toolbar: '#toolbar',
            where:{level:{$level-1},pid:{$pid}},
            cols: [[
                {type: "checkbox", width: 50},
                {field: 'name', width: 180, title: '地区名称'},
                {field: 'merger_name', title: '完整名称', width: 260},
                {field: 'level', title: '等级', width: 80,sort: true},
                {field: 'area_code', title: '行政区号', width: 108,sort: true},
                {field: 'remark', title: '备注说明', width: 180},
                {title: '操作', minWidth: 170, toolbar: '#tableBar'}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 15,
            page: true,
            height: 'full-80',
            done:function() {
                miniTab.listen();
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            table.reload(tableId, { page: { curr: 1} , where: data.field }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar('+tableId+')', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var url = '{:url("propertySystemRegionAdd")}?pid={$pid}&level={$level}';
                var index = parent.layer.open({
                    title: '添加地区信息',
                    type: 2,  shade: 0.2, maxmin:true,
                    area: ['500px', '550px'],
                    content: url , btnAlign: 'c', btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = parent.layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                parent.layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    parent.layer.close(index); //关闭弹层
                                    parent.layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    parent.layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        //监听表格复选框选择
        table.on('checkbox('+tableId+')', function (obj) {
            console.log(obj)
        });

        table.on('tool('+tableId+')', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var url = '{:url("propertySystemRegionEdit")}?id='+data.id;
                var index = parent.layer.open({
                    title: '编辑地区信息', type: 2, shade: 0.2,
                    maxmin:true, area: ['500px', '550px'],
                    content: url  , btnAlign: 'c' , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = parent.window['layui-layer-iframe' + index]
                            , submit = layero.find('iframe').contents().find("#submitBtn");
                        iframeWindow.layui.form.on('submit(submitBtn)', function (data) {
                            var field = data.field; //获取提交的字段
                            var loadAdd = parent.layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                            $.post(url, field, function (res) {
                                parent.layer.close(loadAdd);
                                if (res.code === 0) {
                                    table.reload(tableId); //数据刷新
                                    parent.layer.close(index); //关闭弹层
                                    parent.layer.msg(res.msg, {icon: 6, offset: '15px'});
                                } else {
                                    parent.layer.msg(res.msg, {icon: 5, offset: '15px'});
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                var url = '{:url("propertySystemRegionDel")}';
                layer.confirm('删除后不可恢复！  是否确认? ', {icon: 3}, function () {
                    var loadAdd = layer.msg('提交中', {icon: 16, time: 0, shade: 0.1, offset: '15px'});
                    $.post(url, {id: data.id}, function (res) {
                        layer.close(loadAdd);
                        if (res.code === 0) {
                            table.reload(tableId); //数据刷新
                            layer.msg(res.msg, {icon: 6, offset: '15px'});
                        } else {
                            layer.msg(res.msg, {icon: 5, offset: '15px'});
                        }
                    });
                });
            }
        });
    });
</script>
{/block}